#include <QDebug>
#include <QStringList>
#include "cuslistmodel.h"

ListModelItem::ListModelItem(QObject* parent)
    :QObject(parent)
{

}

ListModelItem::ListModelItem(const ListModelItem &listModelItem)
{

}

ListModelItem::~ListModelItem()
{

}

CusListModel::CusListModel(QObject *parent) : QAbstractListModel(parent)
{
    m_MapItems.clear();
    ListModelItem* item1 = new ListModelItem();
    item1->m_pcDeviceName = ("device1");
    item1->m_pcPrinterState = (1);
    item1->m_pcGroupNo = (1);

    ListModelItem* item2 = new ListModelItem();
    item2->m_pcDeviceName = ("device2");
    item2->m_pcPrinterState = (2);
    item2->m_pcGroupNo = (2);

    ListModelItem* item3 = new ListModelItem();
    item3->m_pcDeviceName = ("device3");
    item3->m_pcPrinterState = (3);
    item3->m_pcGroupNo = (3);

    m_MapItems["111"] = item1;
    m_MapItems["222"] = item2;
    m_MapItems["3333"] = item3;

    //初始化分组信息，从本地文件读取
    m_GroupDevices["默认分组"] = QStringList() << item1->m_pcDeviceName;
    m_GroupDevices["分组1"] = QStringList() << item1->m_pcDeviceName;
    m_GroupDevices["分组2"] = QStringList() << item2->m_pcDeviceName;
    m_GroupDevices["分组3"] = QStringList() << item3->m_pcDeviceName;
}

int CusListModel::rowCount(const QModelIndex &parent) const
{
    return m_MapItems.count();//m_ListItem.count();
}


QVariant CusListModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_MapItems.count())
        return QVariant();

    QVariant res;

    QList<ListModelItem*> listItem = m_MapItems.values();
    ListModelItem* modelInfo = listItem.at(index.row());
    switch (role) {
    case ListModelItem::E_PrinterID:
        res = modelInfo->pcPrinterID();
        break;
    case ListModelItem::E_PrinterState:
        res = modelInfo->pcPrinterState();
        break;
    case ListModelItem::E_HasCamera:
        res = modelInfo->pcHasCamera();
        break;
    case ListModelItem::E_IpAddr:
        res = modelInfo->pcIpAddr();
        break;
    case ListModelItem::E_GCodeName:
        res = modelInfo->pcGCodeName();
        break;
    case ListModelItem::E_TotalPrintTime:
        res = modelInfo->pcTotalPrintTime();
        break;
    case ListModelItem::E_LeftTime:
        res = modelInfo->pcLeftTime();
        break;
    case ListModelItem::E_CurPrintLayer:
        res = modelInfo->pcCurPrintLayer();
        break;
    case ListModelItem::E_BedTemp:
        res = modelInfo->pcBedTemp();
        break;
    case ListModelItem::E_NozzleTemp:
        res = modelInfo->pcNozzleTemp();
        break;
        //需要双向交互的数据直接传递对象指针
    case ListModelItem::E_GroupNo:
    case ListModelItem::E_PrintSpeed:
    case ListModelItem::E_DeviceName:
    case ListModelItem::E_BedDstTemp:
    case ListModelItem::E_NozzleDstTemp:
    case ListModelItem::E_X:
    case ListModelItem::E_Y:
    case ListModelItem::E_Z:
    case ListModelItem::E_FanOpened:
    case ListModelItem::E_LedOpened:
        res = QVariant::fromValue(modelInfo);
        break;
    default:
        ;
    }
    return res;
}


QHash<int, QByteArray> CusListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[ListModelItem::E_PrinterID] = "printerID";
    roles[ListModelItem::E_PrinterState] = "printerState";
    roles[ListModelItem::E_HasCamera] = "hasCamera";
    roles[ListModelItem::E_DeviceName] = "item";
    roles[ListModelItem::E_IpAddr] = "ipAddr";
    roles[ListModelItem::E_GCodeName] = "gCodeName";
    roles[ListModelItem::E_TotalPrintTime] = "totalPrintTime";
    roles[ListModelItem::E_LeftTime] = "leftTime";
    roles[ListModelItem::E_CurPrintLayer] = "curPrintLayer";
    roles[ListModelItem::E_PrintSpeed] = "item";
    roles[ListModelItem::E_BedDstTemp] = "item";
    roles[ListModelItem::E_NozzleDstTemp] = "item";
    roles[ListModelItem::E_BedTemp] = "bedTemp";
    roles[ListModelItem::E_NozzleTemp] = "nozzleTemp";
    roles[ListModelItem::E_GroupNo] = "groupNo";
    roles[ListModelItem::E_X] = "item";
    roles[ListModelItem::E_Y] = "item";
    roles[ListModelItem::E_Z] = "item";
    roles[ListModelItem::E_FanOpened] = "item";
    roles[ListModelItem::E_LedOpened] = "item";
    return roles;
}

void CusListModel::start(const QStringList &deviceIDs)
{
    foreach(auto id, deviceIDs)
    {
        qDebug() << QString("操作设备%1,打印!").arg(id);
    }
}

void CusListModel::pause(const QStringList &deviceIDs)
{
    foreach(auto id, deviceIDs)
    {
        qDebug() << QString("操作设备%1,暂停!").arg(id);
    }
}

void CusListModel::stop(const QStringList &deviceIDs)
{
    foreach(auto id, deviceIDs)
    {
        qDebug() << QString("操作设备%1,停止!").arg(id);
    }
}

void CusListModel::addDevice(const QString &deviceIP)
{
    qDebug() << "请求发现设备:" << deviceIP;
}

void CusListModel::refresh()
{
    qDebug() << "刷新设备列表!";
}

void CusListModel::warmUp(const QStringList &deviceIDs)
{
    foreach(auto id, deviceIDs)
    {
        qDebug() << QString("操作设备%1,预热!").arg(id);
    }
}

void CusListModel::addGroup(const QString& groupName)
{
    m_GroupDevices[groupName] = QStringList();
}

void CusListModel::editGroupName(const QString& beforeGroupName, const QString &groupName)
{
    QStringList deviceIDs = m_GroupDevices[beforeGroupName];
    foreach(auto id, deviceIDs)
    {
        ListModelItem* item = m_MapItems[id];
        item->setPcGroupNo(groupName);
    }
}

QStringList CusListModel::getGroups()
{
    QStringList res = m_GroupDevices.keys();
    return res;
}

int ListModelItem::pcPrinterState() const
{
    return m_pcPrinterState;
}

void ListModelItem::setPcPrinterState(int state)
{
    if (m_pcPrinterState == state)
        return;
    m_pcPrinterState = state;
    emit pcPrinterStateChanged();
}

bool ListModelItem::pcHasCamera() const
{
    return m_pcHasCamera;
}

const QString &ListModelItem::pcDeviceName() const
{
    return m_pcDeviceName;
}

void ListModelItem::setPcDeviceName(const QString& deviceName)
{
    qDebug() << QString("%1:设置该设备的名称为 ").arg(m_pcPrinterID) << deviceName;
    if (m_pcDeviceName == deviceName)
        return;
    m_pcDeviceName = deviceName;
    emit pcDeviceNameChanged();
}

const QString &ListModelItem::pcIpAddr() const
{
    return m_pcIpAddr;
}

const QString &ListModelItem::pcGCodeName() const
{
    return m_pcGCodeName;
}

const QString &ListModelItem::pcTotalPrintTime() const
{
    return m_pcTotalPrintTime;
}

const QString &ListModelItem::pcLeftTime() const
{
    return m_pcLeftTime;
}

int ListModelItem::pcCurPrintLayer() const
{
    return m_pcCurPrintLayer;
}

int ListModelItem::pcPrintSpeed() const
{
    return m_pcPrintSpeed;
}

void ListModelItem::setPcPrintSeed(int speed)
{
    qDebug() << QString("%1:设置该设备的速度为 ").arg(m_pcPrinterID) << speed;
    if (m_pcPrintSpeed == speed)
        return;
    m_pcPrintSpeed = speed;
    emit pcPrintSpeedChanged();
}

int ListModelItem::pcBedDstTemp() const
{
    return m_pcBedDstTemp;
}

void ListModelItem::setPcBedDstTemp(int newPcBedDstTemp)
{
    qDebug() << QString("%1:设置该设备的机床目标温度为 ").arg(m_pcPrinterID) << newPcBedDstTemp;
    if (m_pcBedDstTemp == newPcBedDstTemp)
        return;
    m_pcBedDstTemp = newPcBedDstTemp;
    emit pcBedDstTempChanged();
}

int ListModelItem::pcNozzleDstTemp() const
{
    return m_pcNozzleDstTemp;
}

void ListModelItem::setPcNozzleDstTemp(int newPcNozzleDstTemp)
{
    qDebug() << QString("%1:设置该设备的喷头目标温度为 ").arg(m_pcPrinterID) << newPcNozzleDstTemp;
    if (m_pcNozzleDstTemp == newPcNozzleDstTemp)
        return;
    m_pcNozzleDstTemp = newPcNozzleDstTemp;
    emit pcNozzleDstTempChanged();
}

int ListModelItem::pcBedTemp() const
{
    return m_pcBedTemp;
}

int ListModelItem::pcNozzleTemp() const
{
    return m_pcNozzleTemp;
}

int ListModelItem::pcX() const
{
    return m_pcX;
}

void ListModelItem::setPcX(int newPcX)
{
    qDebug() << QString("%1:设置该设备的X位置为 ").arg(m_pcPrinterID) << newPcX;
    if (m_pcX == newPcX)
        return;
    m_pcX = newPcX;
    emit pcXChanged();
}

int ListModelItem::pcY() const
{
    return m_pcY;
}

void ListModelItem::setPcY(int newPcY)
{
    qDebug() << QString("%1:设置该设备的Y位置为 ").arg(m_pcPrinterID) << newPcY;
    if (m_pcY == newPcY)
        return;
    m_pcY = newPcY;
    emit pcYChanged();
}

int ListModelItem::pcZ() const
{
    return m_pcZ;
}

void ListModelItem::setPcZ(int newPcZ)
{
    qDebug() << QString("%1:设置该设备的Z位置为 ").arg(m_pcPrinterID) << newPcZ;
    if (m_pcZ == newPcZ)
        return;
    m_pcZ = newPcZ;
    emit pcZChanged();
}

void ListModelItem::setPcPrinterID(const QString &newPcPrinterID)
{
    if (m_pcPrinterID == newPcPrinterID)
        return;
    m_pcPrinterID = newPcPrinterID;
    emit pcPrinterIDChanged();
}

const QString &ListModelItem::pcPrinterID() const
{
    return m_pcPrinterID;
}

bool ListModelItem::fanOpened() const
{
    return m_fanOpened;
}

void ListModelItem::setFanOpened(bool newFanOpened)
{
    qDebug() << QString("%1:设置该设备的风扇开关为 ").arg(m_pcPrinterID) << newFanOpened;
    if (m_fanOpened == newFanOpened)
        return;
    m_fanOpened = newFanOpened;
    emit fanOpenedChanged();
}

bool ListModelItem::ledOpened() const
{
    return m_ledOpened;
}

void ListModelItem::setLedOpened(bool newLedOpened)
{
    qDebug() << QString("%1:设置该设备的Led开关为 ").arg(m_pcPrinterID) << newLedOpened;
    if (m_ledOpened == newLedOpened)
        return;
    m_ledOpened = newLedOpened;
    emit ledOpenedChanged();
}

QString ListModelItem::pcGroupNo() const
{
    return m_pcGroupNo;
}

void ListModelItem::setPcGroupNo(const QString& newPcGroupNo)
{
    qDebug() << QString("%1:设置该设备的分组为 ").arg(m_pcPrinterID) << newPcGroupNo;
    if (m_pcGroupNo == newPcGroupNo)
        return;
    m_pcGroupNo = newPcGroupNo;
    emit pcGroupNoChanged();
}

